package app.auto.runner.base.utility.xml;

import android.text.TextUtils;

import app.auto.runner.base.framework.CrashHandler;

public class LineUtil {

    public static String rmLine(String tag, String contain, String replacement) {
        return addline(tag, contain, "", "    ");
    }

    public static String replaceLine(String tag, String contain, String replacement) {
        return addline(tag, contain, "", "    ");
    }

    public static String addline(String tag, String regex, String replacement) {
        return addline(tag, regex, replacement, "");
    }

    public static String addline(String tag, String contain, String replacement, String headspace) {
        return addlinePrivate(tag, contain, contain, replacement, headspace);

    }

    private static String addlinePrivate(String tag, String contain, String regex, String replacement, String headspace) {
        if (tag.contains(replacement) && !replacement.equalsIgnoreCase("")) {
            return tag;
        } else {
            boolean singletag = tag.contains("/>");
            boolean added = false;
            String output = "";
            for (String line : tag.split("\n")) {
                if (!contain.equals("")) {
                    if (line.contains(contain)) {
                        if (line.endsWith("\n") || replacement.startsWith("\n")) {
                            output += replacement;

                        } else {
                            output += "\n" + headspace + replacement;
                        }
                        if (line.contains(">") && !replacement.equalsIgnoreCase("")) {
                            String tail = line.substring(line.indexOf(">"));
                            output = output + tail;
                        }
                        added = true;

                    } else {
                        output += "\n" + line;
                    }
                }

            }

            if (output.startsWith("\n")) {
                output = output.substring(1);
            }
            if (!added) {
                if (singletag) {
                    output = tag.replace("/>", "\n" + replacement + "\n/>");
                } else {
                    try {
                        String tgwd = getTagWord(tag);
                        output = tag.replace("<" + tgwd, "<" + tgwd + "\n" + "    " + replacement);
                        output = output.replace("><", ">\n<");
                    } catch (Exception e) {
                        CrashHandler.errorDialog(e.getCause() + "\n" + e.getMessage());
                    }
                }
            }
            output = output.replaceAll("\n\n", "\n").replaceAll("\n\n\n", "\n").replaceAll("  ", " ");
            tag = output;
        }
        return tag;
    }

    public static String getTagWordGraph(String tag, String tagword) {

        return getTagWordGraphPrivate(tag, tagword);
    }

    private static String getTagWordGraphPrivate(String tag, String tagword) {
        String graph = "";
        int ix, ixend;
        ix = tag.indexOf("<" + tagword);
        if (ix == -1) {
            return "";
        }
        ixend = tag.indexOf("</" + tagword + ">", ix);
        graph = tag.substring(ix, ixend + tagword.length() + 3);
        return graph;
    }

    public static String getTagWord(String tag) {
        String tgwd = "";
        int ix, ixend;
        ix = tag.lastIndexOf("</");
        ixend = tag.indexOf(">", ix);
        tgwd = tag.substring(ix + 2, ixend);
        return tgwd;
    }

    private static String spaces;

    public static String peraddSpaces(String replace) {
        return peraddSpacesPrivate(replace);
    }

    private static String peraddSpacesPrivate(String replace) {
        String[] ls = replace.split("\n");
        String rt = "";
        for (String l : ls) {
            spaces = "\n    ";
            rt += spaces + l;
        }

        rt = rt.substring(spaces.length());
        return rt;
    }

    public static String getLine(String tag, String regex) {
        return getLinePrivate(tag, regex);
    }

    private static String getLinePrivate(String tag, String regex) {
        if (!tag.contains(regex) || TextUtils.isEmpty(regex)) {
            return "";
        } else {
            for (String line : tag.split("\n")) {
                if (line.contains(regex)) {
                    return line;
                }
            }

        }
        return "";
    }
}
